package com.screen.screendisplay.service.impl.ne;

import com.screen.screendisplay.entity.ne.NeSafetyenvCoalReduce;
import com.screen.screendisplay.mapper.ne.NeSafetyenvCoalReduceMapper;
import com.screen.screendisplay.service.ne.NeSafetyenvCoalReduceService;
import com.screen.screendisplay.vo.ne.NeCoalReductionVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 节煤减排量表 服务实现类
 * </p>
 *
 * @author xuesong
 * @since 2025-08-23
 */
@Service
public class NeSafetyenvCoalReduceServiceImpl extends ServiceImpl<NeSafetyenvCoalReduceMapper, NeSafetyenvCoalReduce> implements NeSafetyenvCoalReduceService {

    @Override
    public List<NeCoalReductionVO> getCoalReduction(Integer year) {
        // 获取当前年份
        int currentYear = LocalDate.now().getYear();
        
        // 确定查询的年份范围（最近5年）
        int startYear = currentYear - 4;
        int endYear = currentYear;
        
        // 如果指定了年份参数，则只查询该年份
        if (year != null) {
            startYear = year;
            endYear = year;
        }
        
        // 构建查询条件
        QueryWrapper<NeSafetyenvCoalReduce> queryWrapper = new QueryWrapper<>();
        queryWrapper.between("year", startYear, endYear);
        queryWrapper.orderByDesc("year");
        
        List<NeSafetyenvCoalReduce> entities = this.list(queryWrapper);
        
        // 按年份汇总数据
        Map<Integer, List<NeSafetyenvCoalReduce>> yearMap = entities.stream()
                .collect(Collectors.groupingBy(NeSafetyenvCoalReduce::getYear));
        
        List<NeCoalReductionVO> resultList = new ArrayList<>();
        
        // 生成最近5年的数据（如果没有指定年份）
        if (year == null) {
            for (int i = 0; i < 5; i++) {
                int dataYear = currentYear - i;
                NeCoalReductionVO vo = createYearlyData(dataYear, yearMap.get(dataYear));
                resultList.add(vo);
            }
        } else {
            // 只返回指定年份的数据
            NeCoalReductionVO vo = createYearlyData(year, yearMap.get(year));
            resultList.add(vo);
        }
        
        return resultList;
    }
    
    /**
     * 创建年度汇总数据
     */
    private NeCoalReductionVO createYearlyData(Integer year, List<NeSafetyenvCoalReduce> yearData) {
        NeCoalReductionVO vo = new NeCoalReductionVO();
        vo.setYear(year);
        
        if (yearData != null && !yearData.isEmpty()) {
            // 汇总该年度所有月份和区域的数据
            BigDecimal totalCo2Reduction = yearData.stream()
                    .map(NeSafetyenvCoalReduce::getCo2Reduction)
                    .filter(amount -> amount != null)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            BigDecimal totalCoalSaving = yearData.stream()
                    .map(NeSafetyenvCoalReduce::getCoalSaving)
                    .filter(amount -> amount != null)
                    .reduce(BigDecimal.ZERO, BigDecimal::add);
            
            vo.setCo2Reduction(totalCo2Reduction.setScale(2, BigDecimal.ROUND_HALF_UP));
            vo.setCoalSaving(totalCoalSaving.setScale(2, BigDecimal.ROUND_HALF_UP));
        } else {
            // 如果没有数据，设置为0
            vo.setCo2Reduction(BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP));
            vo.setCoalSaving(BigDecimal.ZERO.setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        
        return vo;
    }

}
